package com.kenyi.co.ui.home;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.ui.home.adapter.MsgContentFragmentAdapter;
import com.kenyi.co.ui.home.bean.IndexItemBean;
import com.kenyi.co.ui.home.ui.SearchActivity;
import com.kenyi.co.utils.NetConfig;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 分类页面
 */
public class HomeFragment extends Fragment implements View.OnClickListener {

    TabLayout tabLayout;
    //自定义视图的全局变量
    private TextView tv_tab;
    private RelativeLayout re_fenlei_search;
    private ImageView iv_home_tab;
    ViewPager viewPager;
    private List<IndexItemBean.DatasBean> names;
    private MsgContentFragmentAdapter adapter;

    public HomeFragment() {

    }

    public static HomeFragment getInstance() {
        return new HomeFragment();
    }

    private View view;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_one, container, false);
        initview();
        adapter = new MsgContentFragmentAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
//        tabLayout.setupWithViewPager(viewPager);


        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIndexItem();

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        initClick();
    }

    private void initview() {
        tabLayout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.view_pager);
        re_fenlei_search = view.findViewById(R.id.re_fenlei_search);
        re_fenlei_search.setOnClickListener(this);

    }


    /**
     * 首页分类
     */
    public void getIndexItem() {

        String url = NetConfig.INDEX_ITEM;
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {


                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        IndexItemBean packlist = gson.fromJson(response, IndexItemBean.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            names = new ArrayList<IndexItemBean.DatasBean>();
                            names = packlist.getDatas();
                            // 更新适配器数据
                            adapter.setList(names);
                            initEvent();
                            initClick();


                        }
                    }
                });
    }

    /**
     * 设置tableLayout的点击监听事件
     */
    private void initClick() {

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //定义方法，判断是否选中
                updateTabView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //定义方法，判断是否选中
                updateTabView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    /**
     * 用来改变tabLayout选中后的字体大小及颜色
     *
     * @param tab
     * @param isSelect
     */
    private void updateTabView(TabLayout.Tab tab, boolean isSelect) {

//找到自定义视图的控件ID
        tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
        iv_home_tab = tab.getCustomView().findViewById(R.id.iv_home_tab);

        if (isSelect) {
            //设置标签选中
            tv_tab.setSelected(true);
            //选中后字体变大
            tv_tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
            tv_tab.setTextColor(this.getResources().getColor(R.color.main_text_FF000000));
            iv_home_tab.setVisibility(View.VISIBLE);

        } else {
            //设置标签取消选中
            tv_tab.setSelected(false);
            //恢复为默认字体大小
            tv_tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            tv_tab.setTextColor(this.getResources().getColor(R.color.main_text_333333));
            iv_home_tab.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置Viewpager的适配器
     */
    private void initEvent() {
        //设置Viewpager的适配器

        tabLayout.setupWithViewPager(viewPager);//TabLayout绑定ViewPager
        tabLayout.setTabsFromPagerAdapter(adapter);//TabLayout的标签从Adapter中获取
        //用来循环适配器中的视图总数
        for (int i = 0; i < adapter.getCount(); i++) {
            //获取每一个tab对象
            TabLayout.Tab tabAt = tabLayout.getTabAt(i);
            //将每一个条目设置我们自定义的视图
            tabAt.setCustomView(R.layout.tablayout_item);
            //默认选中第一个
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                tabAt.getCustomView().findViewById(R.id.tv_tab).setSelected(true);//第一个tab被选中
                //设置选中标签的文字大小
                ((TextView) tabAt.getCustomView().findViewById(R.id.tv_tab)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
                ((TextView) tabAt.getCustomView().findViewById(R.id.tv_tab)).setTextColor(this.getResources().getColor(R.color.main_text_FF000000));
                (tabAt.getCustomView().findViewById(R.id.iv_home_tab)).setVisibility(View.VISIBLE);
            }
            //通过tab对象找到自定义视图的ID
            TextView textView = tabAt.getCustomView().findViewById(R.id.tv_tab);
            textView.setText(names.get(i).getLabel());//设置tab上的文字
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.re_fenlei_search:
                Intent mecontact = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(mecontact);
                break;

        }

    }
}

